# -*- coding: utf-8 -*-

# Created on 06/08/2010

# Copyright (C) 2010  Sergio Rodrigo

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from core.Game import *
from core.Player import *
from pong.PongBall import PongBall

from core.Score import Score

class PongGame (Game):
    """ Main class of the pong game.
    
    * Methods:
    _checkEndConditions(self)
    _onEndAction(self)
    __checkPlayerWin(self, player)
    __checkPlayerPoint(self, player)
    __startNewPoint(self)
    
    * Member variables
    player1 -- Player object. Player 1 of the game
    player2 -- Player object. Player 2 of the game
    ballWallDistance -- integer Distance between the ball and a wall to win a point
    
    Extends Game.
    
    """

    def __init__(self, scene, controlManager):
        """ Constructor. Inits pygame.
        
        * Arguments:
        scene -- Scene object
        controlManager -- ControlManager object
        
        """
        Game.__init__(self, scene, controlManager)
        self.player1 = None
        self.player2 = None
        self.ballWallDistance = 40      # Distance between the ball and a wall to win a point
    
    def _checkEndConditions(self):
        """ Checks the end conditions for the game to be finished.
        
        """
        self.__checkPlayerWin(self.player1)
        self.__checkPlayerWin(self.player2)
        
    def __checkPlayerWin(self, player):
        """ Checks whether the player has win the game.
        
        player -- Player object
        
        """
        score = self.__checkPlayerPoint(player)
        if score:
            score.value = score.value + 1
            print "s.value = ", score.value
            # Force redraw
            self.scene.draw()
            if score.value >= 2:
                print "player ", player.name, " winsssss"
                if player == self.player1:
                    self.player1.state = STATE_WINNER
                    self.player2.state = STATE_LOSSER
                elif player == self.player2:
                    self.player2.state = STATE_WINNER
                    self.player1.state = STATE_LOSSER
                self.state = FINISHED
            else:
                print "paused"
                pygame.time.wait(2000)
                self.__startNewPoint()
        
    def __checkPlayerPoint(self, player):
        """ Checks whether the player has win a point.
        
        player -- Player object
        
        returns -- The score, if it's found.
                   None, otherwise.
        
        """
        for e in self.actors:
            if isinstance(e, PongBall):
                condition = False
                playerTeamScores = None
                scoreName = None
                if player == self.player1:
                    condition = e.x >= self.scene.width - self.ballWallDistance
                    playerTeamScores = self.player1.team.scores
                    scoreName = "score_team_1"
                else: # player == self.player2
                    condition = e.x <= self.ballWallDistance
                    playerTeamScores = self.player2.team.scores
                    scoreName = "score_team_2"
                
                if condition:
                    for s in playerTeamScores:
                        if isinstance(s, Score) == False:
                            return None
                        elif s.name == scoreName:
                            return s
                    
        return None
                    
    def __startNewPoint(self):
        """ Resets the position/state of the elements to start a new point.
        
        """
        for e in self.actors:
            if isinstance(e, PongBall):
                e.x = self.scene.width / 2
                e.y = self.scene.height / 2

# eof
